// pages/operation/renewalAlert/settingAlert/index.js
import { RenewalAlert } from "../../../../api/api-work/renewalAlert.js";
const renewalAlert = new RenewalAlert();
import Toast from "@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: "1",
    id: null,
    remainingClass: 0,
    remainingDay: 0,
    ruleText: "",
    isShowRuleType: false,
    ruleTypeList: [
      {
        lable: "1",
        value: "满足以上其中一个条件",
      },
      {
        lable: "2",
        value: "同时满足以上两个条件",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(options.param);
    console.log("options.param", options.param);
    this.setData({
      type: params.type,
      id: params.id,
      remainingClass: params.remainingClass,
      remainingDay: params.remainingDay,
      ruleText:
        params.type === "1"
          ? "满足以上其中一个条件将进入续费预警列表"
          : "同时满足以上两个条件将进入续费预警列表",
    });
  },
  onChange1(e) {
    this.setData({
      remainingClass: e.detail,
    });
  },
  onChange2(e) {
    this.setData({
      remainingDay: e.detail,
    });
  },
  //打开预警条件弹窗
  showRuleType() {
    this.setData({
      isShowRuleType: true,
    });
  },
  //关闭预警条件弹窗
  onCloseRuleType() {
    this.setData({
      isShowRuleType: false,
    });
  },
  //选择预警条件
  selectRuleType(e) {
    const query = e.currentTarget.dataset.query;
    console.log("query: ", query);
    this.setData({
      type: query.lable,
      ruleText:
        query.lable === "1"
          ? "满足以上其中一个条件将进入续费预警列表"
          : "同时满足以上两个条件将进入续费预警列表",
      isShowRuleType: false,
    });
  },

  //设置预警条件
  async saveAlertSettings() {
    renewalAlert
      .saveAlertSettingsAPI({
        type: this.data.type,
        id: this.data.id,
        remainingClass: this.data.remainingClass,
        remainingDay: this.data.remainingDay,
      })
      .then((res) => {
        Toast("预警条件设置成功");
        setTimeout(() => {
          wx.navigateBack();
        }, 1000);
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
Component({
  data: {
    type: "1",
    id: null,
    remainingClass: 0,
    remainingDay: 0,
    ruleText: "",
    isShowRuleType: false,
    ruleTypeList: [
      {
        lable: "1",
        value: "满足以上其中一个条件",
      },
      {
        lable: "2",
        value: "同时满足以上两个条件",
      },
    ],
  },
  created() {
    this.getSettingsList();
  },
  methods: {
    getSettingsList() {
      renewalAlert.getSettingsListAPI().then((res) => {
        this.setData({
          type: res.type,
          id: res.id,
          remainingClass: res.remainingClass,
          remainingDay: res.remainingDay,
          ruleText:
            res.type === "1"
              ? "满足以上其中一个条件将进入续费预警列表"
              : "同时满足以上两个条件将进入续费预警列表",
        });
      });
    },
    onChange1(e) {
      this.setData({
        remainingClass: e.detail,
      });
    },
    onChange2(e) {
      this.setData({
        remainingDay: e.detail,
      });
    },
    //打开预警条件弹窗
    showRuleType() {
      this.setData({
        isShowRuleType: true,
      });
    },
    //关闭预警条件弹窗
    onCloseRuleType() {
      this.setData({
        isShowRuleType: false,
      });
    },
    //选择预警条件
    selectRuleType(e) {
      const query = e.currentTarget.dataset.query;
      console.log("query: ", query);
      this.setData({
        type: query.lable,
        ruleText:
          query.lable === "1"
            ? "满足以上其中一个条件将进入续费预警列表"
            : "同时满足以上两个条件将进入续费预警列表",
        isShowRuleType: false,
      });
    },

    //设置预警条件
    async saveAlertSettings() {
      renewalAlert
        .saveAlertSettingsAPI({
          type: this.data.type,
          id: this.data.id,
          remainingClass: this.data.remainingClass,
          remainingDay: this.data.remainingDay,
        })
        .then((res) => {
          Toast("预警条件设置成功");
        });
    },
  },
});
